diff --git a/doc/src/sgml/ref/ecpg-ref.sgml b/doc/src/sgml/ref/ecpg-ref.sgml index 8bfb47c4d7..a6d38a3daa 100644 --- a/doc/src/sgml/ref/ecpg-ref.sgml +++ b/doc/src/sgml/ref/ecpg-ref.sgml @@ -95,6 +95,26 @@ PostgreSQL documentation + + + + Enables the comment in SQL statements. If this option is not specified, + the comment will be removed as a result of the ecpg precompile and be + disabled. The SQL statements that can be specified in comment are + INSERT, UPDATE, and + DELETE. The locations in which the comment can + be specified are immediately after one of the SELECT, + INSERT, UPDATE, DELETE, + or WITH keywords. A syntax error will occur if any other + location is specified. An example keeping the comment in SQL statement. + +EXEC SQL SELECT /* qid=3, at line 30 in yourApp.ecpg */ * INTO :C1, :C2 FROM T1 WHERE C1=1; + + + + + + diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index e833b2eba5..31267d9098 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -574,7 +574,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); %type partbound_datum_list %type PartitionRangeDatum %type range_datum_list - +%type parse_comment /* * Non-keyword token types. These are hard-wired into the "flex" lexer. * They must be listed first so that their numeric codes do not depend on @@ -588,6 +588,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); %token ICONST PARAM %token TYPECAST DOT_DOT COLON_EQUALS EQUALS_GREATER %token LESS_EQUALS GREATER_EQUALS NOT_EQUALS +%token SQL_PARSE_COMMENT /* * If you want to make any keyword changes, update the keyword table in @@ -10314,14 +10315,14 @@ DeallocateStmt: DEALLOCATE name *****************************************************************************/ InsertStmt: - opt_with_clause INSERT INTO insert_target insert_rest + opt_with_clause INSERT parse_comment INTO insert_target insert_rest opt_on_conflict returning_clause { - $5->relation = $4; - $5->onConflictClause = $6; - $5->returningList = $7; - $5->withClause = $1; - $$ = (Node *) $5; + $6->relation = $5; + $6->onConflictClause = $7; + $6->returningList = $8; + $6->withClause = $1; + $$ = (Node *) $6; } ; @@ -10445,14 +10446,14 @@ returning_clause: * *****************************************************************************/ -DeleteStmt: opt_with_clause DELETE_P FROM relation_expr_opt_alias +DeleteStmt: opt_with_clause DELETE_P parse_comment FROM relation_expr_opt_alias using_clause where_or_current_clause returning_clause { DeleteStmt *n = makeNode(DeleteStmt); - n->relation = $4; - n->usingClause = $5; - n->whereClause = $6; - n->returningList = $7; + n->relation = $5; + n->usingClause = $6; + n->whereClause = $7; + n->returningList = $8; n->withClause = $1; $$ = (Node *)n; } @@ -10514,18 +10515,18 @@ opt_nowait_or_skip: * *****************************************************************************/ -UpdateStmt: opt_with_clause UPDATE relation_expr_opt_alias +UpdateStmt: opt_with_clause UPDATE parse_comment relation_expr_opt_alias SET set_clause_list from_clause where_or_current_clause returning_clause { UpdateStmt *n = makeNode(UpdateStmt); - n->relation = $3; - n->targetList = $5; - n->fromClause = $6; - n->whereClause = $7; - n->returningList = $8; + n->relation = $4; + n->targetList = $6; + n->fromClause = $7; + n->whereClause = $8; + n->returningList = $9; n->withClause = $1; $$ = (Node *)n; } @@ -10767,33 +10768,33 @@ select_clause: * However, this is not checked by the grammar; parse analysis must check it. */ simple_select: - SELECT opt_all_clause opt_target_list + SELECT parse_comment opt_all_clause opt_target_list into_clause from_clause where_clause group_clause having_clause window_clause { SelectStmt *n = makeNode(SelectStmt); - n->targetList = $3; - n->intoClause = $4; - n->fromClause = $5; - n->whereClause = $6; - n->groupClause = $7; - n->havingClause = $8; - n->windowClause = $9; + n->targetList = $4; + n->intoClause = $5; + n->fromClause = $6; + n->whereClause = $7; + n->groupClause = $8; + n->havingClause = $9; + n->windowClause = $10; $$ = (Node *)n; } - | SELECT distinct_clause target_list + | SELECT parse_comment distinct_clause target_list into_clause from_clause where_clause group_clause having_clause window_clause { SelectStmt *n = makeNode(SelectStmt); - n->distinctClause = $2; - n->targetList = $3; - n->intoClause = $4; - n->fromClause = $5; - n->whereClause = $6; - n->groupClause = $7; - n->havingClause = $8; - n->windowClause = $9; + n->distinctClause = $3; + n->targetList = $4; + n->intoClause = $5; + n->fromClause = $6; + n->whereClause = $7; + n->groupClause = $8; + n->havingClause = $9; + n->windowClause = $10; $$ = (Node *)n; } | values_clause { $$ = $1; } @@ -10841,24 +10842,24 @@ simple_select: * Recognizing WITH_LA here allows a CTE to be named TIME or ORDINALITY. */ with_clause: - WITH cte_list + WITH parse_comment cte_list { $$ = makeNode(WithClause); - $$->ctes = $2; + $$->ctes = $3; $$->recursive = false; $$->location = @1; } - | WITH_LA cte_list + | WITH_LA parse_comment cte_list { $$ = makeNode(WithClause); - $$->ctes = $2; + $$->ctes = $3; $$->recursive = false; $$->location = @1; } - | WITH RECURSIVE cte_list + | WITH parse_comment RECURSIVE cte_list { $$ = makeNode(WithClause); - $$->ctes = $3; + $$->ctes = $4; $$->recursive = true; $$->location = @1; } @@ -10885,6 +10886,11 @@ opt_with_clause: | /*EMPTY*/ { $$ = NULL; } ; +parse_comment: + SQL_PARSE_COMMENT { $$ = NULL; } + | /*EMPTY*/ { $$ = NULL; } + ; + into_clause: INTO OptTempTableName { diff --git a/src/interfaces/ecpg/preproc/ecpg.addons b/src/interfaces/ecpg/preproc/ecpg.addons index ca3efadc48..d8397ca412 100644 --- a/src/interfaces/ecpg/preproc/ecpg.addons +++ b/src/interfaces/ecpg/preproc/ecpg.addons @@ -324,6 +324,15 @@ ECPG: DeclareCursorStmtDECLAREcursor_namecursor_optionsCURSORopt_holdFORSelectSt /* We put this text into a comment, so we better remove [*][/]. */ c2[0] = '.'; c2[1] = '.'; + + if (enable_parse_comment == true) + { + while ((c2 = strstr(c2, "*/"))) + { + c2[0] = '.'; + c2[1] = '.'; + } + } } comment = cat_str(3, mm_strdup("/*"), c1, mm_strdup("*/")); diff --git a/src/interfaces/ecpg/preproc/ecpg.c b/src/interfaces/ecpg/preproc/ecpg.c index fa80bb289e..9a0978c976 100644 --- a/src/interfaces/ecpg/preproc/ecpg.c +++ b/src/interfaces/ecpg/preproc/ecpg.c @@ -18,7 +18,8 @@ bool autocommit = false, force_indicator = true, questionmarks = false, regression_mode = false, - auto_prepare = false; + auto_prepare = false, + enable_parse_comment = false; char *output_filename; @@ -54,6 +55,7 @@ help(const char *progname) " \"no_indicator\", \"prepare\", \"questionmarks\"\n")); printf(_(" --regression run in regression testing mode\n")); printf(_(" -t turn on autocommit of transactions\n")); + printf(_(" --enable-parse-comment enable a block comment /* ... /* in the SQL statement\n")); printf(_(" -V, --version output version information, then exit\n")); printf(_(" -?, --help show this help, then exit\n")); printf(_("\nIf no output file is specified, the name is formed by adding .c to the\n" @@ -112,11 +114,13 @@ add_preprocessor_define(char *define) } #define ECPG_GETOPT_LONG_REGRESSION 1 +#define ECPG_GETOPT_LONG_ENABLE_PARSE_COMMENT 2 int main(int argc, char *const argv[]) { static struct option ecpg_options[] = { {"regression", no_argument, NULL, ECPG_GETOPT_LONG_REGRESSION}, + {"enable-parse-comment", no_argument, NULL, ECPG_GETOPT_LONG_ENABLE_PARSE_COMMENT}, {NULL, 0, NULL, 0} }; @@ -238,6 +242,9 @@ main(int argc, char *const argv[]) progname); #endif break; + case ECPG_GETOPT_LONG_ENABLE_PARSE_COMMENT: + enable_parse_comment = true; + break; default: fprintf(stderr, _("Try \"%s --help\" for more information.\n"), argv[0]); return ILLEGAL_OPTION; diff --git a/src/interfaces/ecpg/preproc/ecpg.trailer b/src/interfaces/ecpg/preproc/ecpg.trailer index 1c108795de..05519edd33 100644 --- a/src/interfaces/ecpg/preproc/ecpg.trailer +++ b/src/interfaces/ecpg/preproc/ecpg.trailer @@ -1766,6 +1766,8 @@ quoted_ident_stringvar: name { $$ = make3_str(mm_strdup("("), $1, mm_strdup(")")); } ; +ecpg_parse_comment: SQL_PARSE_COMMENT { $$ = $1; } ; + /* * C stuff */ diff --git a/src/interfaces/ecpg/preproc/ecpg.type b/src/interfaces/ecpg/preproc/ecpg.type index ac6aa000ac..5fac6cc1a9 100644 --- a/src/interfaces/ecpg/preproc/ecpg.type +++ b/src/interfaces/ecpg/preproc/ecpg.type @@ -66,6 +66,7 @@ %type ecpg_sconst %type ecpg_using %type ecpg_xconst +%type ecpg_parse_comment %type enum_definition %type enum_type %type execstring @@ -143,3 +144,5 @@ %type var_type %type action + +%type SQL_PARSE_COMMENT diff --git a/src/interfaces/ecpg/preproc/extern.h b/src/interfaces/ecpg/preproc/extern.h index 0ce3a6e424..8c1c035a32 100644 --- a/src/interfaces/ecpg/preproc/extern.h +++ b/src/interfaces/ecpg/preproc/extern.h @@ -25,7 +25,8 @@ extern bool autocommit, force_indicator, questionmarks, regression_mode, - auto_prepare; + auto_prepare, + enable_parse_comment; extern int braces_open, ret_value, struct_level, diff --git a/src/interfaces/ecpg/preproc/parse.pl b/src/interfaces/ecpg/preproc/parse.pl index 8a401304ec..18d315fcbf 100644 --- a/src/interfaces/ecpg/preproc/parse.pl +++ b/src/interfaces/ecpg/preproc/parse.pl @@ -38,7 +38,8 @@ my %replace_token = ( 'FCONST' => 'ecpg_fconst', 'Sconst' => 'ecpg_sconst', 'IDENT' => 'ecpg_ident', - 'PARAM' => 'ecpg_param',); + 'PARAM' => 'ecpg_param', + 'SQL_PARSE_COMMENT' => 'ecpg_parse_comment',); # or in the block my %replace_string = ( diff --git a/src/interfaces/ecpg/preproc/pgc.l b/src/interfaces/ecpg/preproc/pgc.l index b894a33e53..d6a91fe90a 100644 --- a/src/interfaces/ecpg/preproc/pgc.l +++ b/src/interfaces/ecpg/preproc/pgc.l @@ -117,6 +117,7 @@ static struct _if_value * $foo$ quoted strings * quoted identifier with Unicode escapes * quoted string with Unicode escapes + * enable parser comments in SQL statement */ %x xb @@ -134,6 +135,7 @@ static struct _if_value %x xskip %x xui %x xus +%x xpc /* Bit string */ @@ -382,6 +384,37 @@ cppline {space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+ {whitespace} { /* ignore */ } +{xcstart} { + xcdepth++; + /* Put back any characters past slash-star; see above */ + yyless(2); + addlit(yytext, yyleng); + } + +{xcstop} { + addlit(yytext, yyleng); + if (xcdepth <= 0) + { + BEGIN(state_before); + token_start = NULL; + base_yylval.str = mm_strdup(literalbuf); + return SQL_PARSE_COMMENT; + } + else + { + xcdepth--; + } + } + +{xcinside} { + addlit(yytext, yyleng); + } +{op_chars} { + addlit(yytext, yyleng); + } + +<> { mmfatal(PARSE_ERROR, "unterminated /* comment in SQL statement"); } + {xcstart} { token_start = yytext; state_before = YYSTATE; @@ -392,13 +425,26 @@ cppline {space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+ fputs("/*", yyout); } {xcstart} { - token_start = yytext; - state_before = YYSTATE; - xcdepth = 0; - BEGIN(xcsql); - /* Put back any characters past slash-star; see above */ - yyless(2); - fputs("/*", yyout); + if(enable_parse_comment == true) + { + token_start = yytext; + state_before = YYSTATE; + xcdepth = 0; + BEGIN(xpc); + yyless(3); + startlit(); + addlit(yytext, yyleng); + } + else + { + token_start = yytext; + state_before = YYSTATE; + xcdepth = 0; + BEGIN(xcsql); + /* Put back any characters past slash-star; see above */ + yyless(2); + fputs("/*", yyout); + } } {xcstart} { ECHO; } {xcstart} {